package com.makenv.web.portal;

import com.makenv.domain.Msg;
import com.makenv.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/msg")
public class MsgController {

    @Autowired
    MsgService msgService;

    //每次显示的数据
    private int row;

    //总的页数
    private int pageSum;

    @RequestMapping("/show")
    public String show(HttpServletRequest request) {

        int num = msgService.getMsgSum();
        List<Msg> msgList = msgService.showMsgList(0, num);
        request.setAttribute("msgList", msgList);
        return "front/message";
    }

    @RequestMapping("/add")
    public String add(@RequestParam String name, @RequestParam(required = false) String email,
                      @RequestParam(required = false) String website, @RequestParam String content) {
        msgService.insert(name, email, website, content);
        return "redirect:/msg/show";
    }

    @RequestMapping("/addreply")
    public String addReplay(@RequestParam String precontent, @RequestParam String name, @RequestParam(required = false) String email,
                            @RequestParam(required = false) String website, @RequestParam String content) {
        msgService.insert(name, email, website, precontent + content);
        return "redirect:/msg/show";
    }

}
